# -*- coding:utf-8 -*-

import pygame
import random
import math
import copy
from pygame.locals import *

width = 1200
height = 800

radius = 15
march_step = 2

start_pos = [10, 10]
end_pos = [200, 200]


class GapWall:
    def __init__(self, x, gap):
        """
        init the march
        """
        self.x = x
        self.gap = gap  # 缺口中心位置y坐标


# s = 1/ 2 * g *t^2

total_count = 0


def get_new_pos(run_count):
    return [start_pos[0] + march_step * total_count, int(round(start_pos[1] + float(run_count * run_count) / 20))]


wall_distance = 300
wall_width = 20
wall_gap = 300

bomb_col = 10
bomb_gap = 30

bomb_width = 100
bomb_height = 30

plane_width = 100
plane_height = 10


# 每隔一段距离生成固定宽度缺口的墙壁
def gen_wall():
    wall_list = []
    for x in range(wall_distance, width, wall_distance):
        gapwall = GapWall(x, random.randint(0 + wall_gap / 2, height - wall_gap / 2))
        wall_list.append(gapwall)
    return wall_list


def main():
    pygame.init()
    clock = pygame.time.Clock()

    screen = pygame.display.set_mode([width, height])

    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((255, 255, 255))

    restart = True
    while restart:

        global total_count

        screentitle = pygame.display.set_caption("free fall")

        bomb_list = []

        plane_col = 0

        first_bomb_y = 0

        running = True
        while running:
            # fill the background is white
            screen.fill([255, 255, 255])

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit(0)
                elif event.type == MOUSEBUTTONDOWN:
                    pos = pygame.mouse.get_pos()
                    mouse_x = pos[0]
                    if mouse_x > (width / bomb_col * plane_col + plane_width):
                        plane_col = plane_col + 1
                    elif mouse_x < (width / bomb_col * plane_col):
                        plane_col = plane_col - 1

            time_pass = clock.tick(20)

            #  绘制炸弹
            bomb_rect_list = []
            for index in range(0, len(bomb_list)):
                bomb = bomb_list[index]
                # bomb_rect = pygame.draw.rect(screen, (0, 255, 0), ((bomb * width / bomb_col, bomb_gap * index), (bomb_width, bomb_height)))
                bomb_rect = pygame.draw.rect(screen, (0, 255, 0), (
                (bomb * width / bomb_col, first_bomb_y - bomb_gap * index), (bomb_width, bomb_height)))
                bomb_rect_list.append(bomb_rect)

            # bomb_list.insert(0, random.randint(0, bomb_col - 1))
            bomb_list.append(random.randint(0, bomb_col - 1))

            # if len(bomb_list) > height / bomb_gap:
            #     bomb_list = bomb_list[1:]

            plane_rect = pygame.draw.rect(screen, (0, 0, 255),
                                          ((plane_col * (width / bomb_col), height - plane_height),
                                           (plane_width, plane_height)))

            first_bomb_y = first_bomb_y + 5

            if first_bomb_y > height:
                first_bomb_y = first_bomb_y - bomb_gap
                bomb_list = bomb_list[1:]

            for bomb in bomb_rect_list:
                if bomb.colliderect(plane_rect):
                    running = False

            pygame.display.update()
        # game over background
        pygame.font.init()
        screen.fill([100, 0, 0])
        font = pygame.font.Font(None, 48)
        text = font.render("Game Over !!!", True, (255, 0, 0))
        textRect = text.get_rect()
        textRect.centerx = screen.get_rect().centerx
        textRect.centery = screen.get_rect().centery + 24
        screen.blit(text, textRect)
        # keydown r restart,keydown n exit
        while 1:
            event = pygame.event.poll()
            if event.type == pygame.QUIT:
                pygame.quit()
                exit(0)
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    restart = True
                    break
                if event.key == pygame.K_n:
                    restart = False
                    break
            pygame.display.update()


if __name__ == '__main__':
    main()
